package di

import (
	"fmt"
	"github.com/mix-go/xdi"
	"github.com/yupeng2015/xsql"
	"hy_808/configor"
	"time"
)

func init() {
	var db *xsql.DB
	switch configor.ConfigGlobal.Database.Present {
	case "Oracle":
	case "Mssql":
		db, _ = InitMssql()
		//定时检测连接是否超时
		go func() {
			rawDB := db.GetRawDB()
			for {
				err := rawDB.Ping()
				if err != nil {
					fmt.Println("数据库连接失败,尝试重连", err)
				}
				time.Sleep(5 * time.Second)
			}
		}()
	case "Mysql":
	}

	obj := xdi.Object{
		Name: "db",
		New: func() (interface{}, error) {
			return db, nil
		},
	}
	if err := xdi.Provide(&obj); err != nil {
		panic(err)
	}

}

func TakeDB() (db *xsql.DB) {
	if err := xdi.Populate("db", &db); err != nil {
		panic(err)
	}
	return
}
